﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class EquipmentType : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chbIsActive;
        protected HtmlForm F2241;
        protected HtmlInputHidden hdnFrom;
        protected Label lblCubicCapacity;
        protected Label lblDescr;
        protected Label lblEquipmentID;
        protected Label lblHeight;
        protected Label lblLength;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblWeightCapacity;
        protected Label lblWidth;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtCubicCapactiy;
        protected TextBox txtDescr;
        protected TextBox txtEquipmentID;
        protected TextBox txtHeight;
        protected TextBox txtLength;
        protected TextBox txtWeightCapacity;
        protected TextBox txtWidth;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtEquipmentID.Text = "";
            this.txtEquipmentID.Enabled = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtEquipmentID.Text.Trim() != "")
                {
                    List<string> equipmentIDs = new List<string> {
                        this.txtEquipmentID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(equipmentIDs, "EQUIPMENT_TYPE") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                else
                {
                    this.lblMsg.Text = "没有保存,不能删除";
                }
            }
            catch (Exception)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + "此车辆类型有车辆记录,请先删除此类型的车辆的信息再删除此车辆类型。";
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("EquipmentTypeQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("EquipmentTypeList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtEquipmentID.Text.Trim() == "")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2530_001");
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateEquipmentTypeInfo();
            }
            else
            {
                this.InsertEquipmentTypeInfo();
            }
        }

        private EquipmentTypeInfo GetEquipmentTypeInfo(EquipmentTypeInfo equipmentTypeInfo)
        {
            decimal decTemp = new decimal(0, 0, 0, false, 1);
            equipmentTypeInfo.EquipmentTypeID = this.txtEquipmentID.Text.Trim();
            if (decimal.TryParse(this.txtCubicCapactiy.Text.Trim(), out decTemp))
            {
                equipmentTypeInfo.CubicCapacity = decimal.Parse(this.txtCubicCapactiy.Text.Trim());
            }
            else
            {
                equipmentTypeInfo.CubicCapacity = decTemp;
            }
            if (decimal.TryParse(this.txtWeightCapacity.Text.Trim(), out decTemp))
            {
                equipmentTypeInfo.WeightCapacity = decimal.Parse(this.txtWeightCapacity.Text.Trim());
            }
            else
            {
                equipmentTypeInfo.WeightCapacity = decTemp;
            }
            if (decimal.TryParse(this.txtLength.Text.Trim(), out decTemp))
            {
                equipmentTypeInfo.Length = decimal.Parse(this.txtLength.Text.Trim());
            }
            else
            {
                equipmentTypeInfo.Length = decTemp;
            }
            if (decimal.TryParse(this.txtWidth.Text.Trim(), out decTemp))
            {
                equipmentTypeInfo.Width = decimal.Parse(this.txtWidth.Text.Trim());
            }
            else
            {
                equipmentTypeInfo.Width = decTemp;
            }
            if (decimal.TryParse(this.txtHeight.Text.Trim(), out decTemp))
            {
                equipmentTypeInfo.Height = decimal.Parse(this.txtHeight.Text.Trim());
            }
            else
            {
                equipmentTypeInfo.Height = decTemp;
            }
            equipmentTypeInfo.Descr = this.txtDescr.Text.Trim();
            equipmentTypeInfo.IsActive = this.chbIsActive.Checked ? "Y" : "N";
            return equipmentTypeInfo;
        }

        private void InsertEquipmentTypeInfo()
        {
            try
            {
                EquipmentTypeInfo equipmentTypeInfo = new EquipmentTypeInfo();
                equipmentTypeInfo = this.GetEquipmentTypeInfo(equipmentTypeInfo);
                equipmentTypeInfo.CreatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(equipmentTypeInfo, "EQUIPMENT_TYPE") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = equipmentTypeInfo;
                this.txtEquipmentID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetEquipmentTypeInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetEquipmentTypeInfo2Form()
        {
            try
            {
                string equipmentTypeID = base.Request.QueryString["ID"].ToString();
                EquipmentTypeInfo equipmentType = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(equipmentTypeID, "EQUIPMENT_TYPE") as EquipmentTypeInfo;
                base.DataEntityViewState = equipmentType;
                if (equipmentType != null)
                {
                    this.txtEquipmentID.Text = equipmentType.EquipmentTypeID;
                    this.txtEquipmentID.Enabled = false;
                    this.txtCubicCapactiy.Text = base.GetPreferenceDigit(equipmentType.CubicCapacity.ToString());
                    this.txtWeightCapacity.Text = base.GetPreferenceDigit(equipmentType.WeightCapacity.ToString());
                    this.txtLength.Text = base.GetPreferenceDigit(equipmentType.Length.ToString());
                    this.txtWidth.Text = base.GetPreferenceDigit(equipmentType.Width.ToString());
                    this.txtHeight.Text = base.GetPreferenceDigit(equipmentType.Height.ToString());
                    this.txtDescr.Text = equipmentType.Descr;
                    this.chbIsActive.Checked = equipmentType.IsActive == "Y";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateEquipmentTypeInfo()
        {
            try
            {
                EquipmentTypeInfo equipmentTypeInfo = base.DataEntityViewState as EquipmentTypeInfo;
                equipmentTypeInfo = this.GetEquipmentTypeInfo(equipmentTypeInfo);
                equipmentTypeInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(equipmentTypeInfo, "EQUIPMENT_TYPE") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

